package com.clear.state.fsm.branch;

public class Mario {
    private MarioState state;

    public Mario() {
        state = MarioState.SMALL;
    }

    public void handleEvent(Event event) {
        MarioState newState = state;
        if (state == MarioState.DEAD) {
            return;
        }
        // 处理吃蘑菇时间：不同状态下吃蘑菇会有不同的反应
        if (event == Event.MUSHROOM) {
            // 只有小马里奥吃蘑菇，才会变大
            if (state == MarioState.SMALL) {
                newState = MarioState.BIG;
            }
        }
        // 处理吃火花事件
        else if (event == Event.FIRE_FLOWER) {
            // 只有大马里奥吃火花，才会变火马里奥
            if (state == MarioState.BIG) {
                newState = MarioState.FIRE;
            }
        }
        // 处理遇到小怪事件
        else if (event == Event.ENEMY_ATTACK) {
            if (state == MarioState.SMALL) {
                newState = MarioState.DEAD;
            } else if (state == MarioState.BIG) {
                newState = MarioState.SMALL;
            } else if (state == MarioState.FIRE) {
                newState = MarioState.BIG;
            }
        }
        // 处理掉坑事件
        else if (event == Event.FALL_INTO_PIT) {
            newState = MarioState.DEAD;
        }

        System.out.printf("从 %s 变为 %s%n", state, newState);
        state = newState;
    }
}